package com.mz.rabbit;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    @Value("${chaohi.rabbitmq.host}")
    private String post;

    @Value("${chaohi.rabbitmq.port}")
    private int port;

    @Value("${chaohi.rabbitmq.username}")
    private String username;

    @Value("${chaohi.rabbitmq.password}")
    private String password;

    @Value("${chaohi.rabbitmq.publisher-confirms}")
    private boolean publisherConfirms;

    @Value("${chaohi.rabbitmq.virtual-host}")
    private String virtualHost;

    @Value("${chaohi.rabbitmq.consumers.amount}")
    private int consumersAmount;
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("hello-test");
    }

    @Bean
    public Binding binding_cart_alarm_empty() {
        return BindingBuilder.bind(helloQueue()).to(fanoutExchange());
    }
    /**
     * 创建数据源
     * @return
     */
    @Bean
    public CachingConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(post);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirms(publisherConfirms); //必须要设置
        return connectionFactory;
    }

    /**
     * 消息监听container
     * @return
     */
    @Bean( name = "pointTaskContainerFactory" )
    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(10);
        factory.setConcurrentConsumers(50);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认
        configurer.configure(factory, connectionFactory());
        return factory;
    }
    //---------并发处理receiver end

}
